package com.das.mechanic_base.adapter.create;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.kproduce.roundcorners.RoundImageView;

import java.util.ArrayList;
import java.util.List;

public class AddStaffAdapter extends RecyclerView.Adapter<AddStaffAdapter.AddStaffHolder> {

    private List<AddStaffBean> mList;

    private List<Long> sList;

    private Context mContext;

    public AddStaffAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.sList = new ArrayList<>();
    }

    public void changeStaff(List<AddStaffBean> mList) {
        this.mList = mList;
        this.sList.clear();
        for (int i = 0; i < mList.size(); i++) {
            if (!mList.get(i).selected) {
                continue;
            }
            sList.add(mList.get(i).id);
        }
        if (iOnClickSelectStaff != null) {
            iOnClickSelectStaff.iOnSelectStaff(sList);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AddStaffHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AddStaffHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_add_staff_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull AddStaffHolder holder, int position) {

        X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(mList.get(position).portrait),holder.rv_staff,R.mipmap.x3_car_owner_header);

        holder.tv_staff_name.setText(mList.get(position).name);

        holder.iv_select.setImageResource(mList.get(position).selected ? R.mipmap.x3_car_select : R.mipmap.x3_car_no_select);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddStaffBean staffBean = mList.get(position);
                boolean selected = staffBean.selected;
//                if (!selected && sList.size() >= 2) {
//                    X3ToastUtils.showMessage(mContext.getString(R.string.x3_car_detail_select_staff_limit));
//                    return;
//                }
                staffBean.selected = !selected;
                mList.remove(position);
                mList.add(position,staffBean);
                notifyItemChanged(position);
                sList.clear();
                for (int i = 0; i < mList.size(); i++) {
                    if (!mList.get(i).selected) {
                        continue;
                    }
                    sList.add(mList.get(i).id);
                }
                if (iOnClickSelectStaff != null) {
                    iOnClickSelectStaff.iOnSelectStaff(sList);
                }
            }
        });
    }

    IOnClickSelectStaff iOnClickSelectStaff;

    public AddStaffAdapter setiOnClickSelectStaff(IOnClickSelectStaff iOnClickSelectStaff) {
        this.iOnClickSelectStaff = iOnClickSelectStaff;
        return this;
    }

    public interface IOnClickSelectStaff {
        void iOnSelectStaff(List<Long> sList);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class AddStaffHolder extends RecyclerView.ViewHolder {
        RoundImageView rv_staff;
        TextView tv_staff_name;
        ImageView iv_select;
        public AddStaffHolder(@NonNull View itemView) {
            super(itemView);
            rv_staff = itemView.findViewById(R.id.rv_staff);
            tv_staff_name = itemView.findViewById(R.id.tv_staff_name);
            iv_select = itemView.findViewById(R.id.iv_select);

        }
    }
}
